package br.com.ufrn.aipetro.ga.controller.maintenance;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;

import br.com.ufrn.aipetro.ga.controller.interfaces.AbstractController;
import br.com.ufrn.aipetro.ga.domain.entity.device.Device;
import br.com.ufrn.aipetro.ga.domain.entity.device.controller.Controller;
import br.com.ufrn.aipetro.ga.domain.entity.device.maintenance.Cause;
import br.com.ufrn.aipetro.ga.domain.entity.device.maintenance.Maintenance;
import br.com.ufrn.aipetro.ga.domain.entity.device.maintenance.MaintenanceType;
import br.com.ufrn.aipetro.ga.domain.entity.device.maintenance.Team;
import br.com.ufrn.aipetro.ga.domain.entity.mesh.Mesh;
import br.com.ufrn.aipetro.ga.domain.entity.plant.Plant;
import br.com.ufrn.aipetro.ga.domain.entity.unit.Unit;
import br.com.ufrn.aipetro.ga.persist.interfaces.DaoFactory;

public class MaintenanceController extends AbstractController<Maintenance> {
	
	DaoFactory daoFactory;
	
	public MaintenanceController(DaoFactory factory) {
		this.daoFactory = factory;
		setModel(new Maintenance());
	}
	
	public void changeTitle(String title) {
		getModel().setTitle(title);
	}
	
	public void changeDateStart(Date datestart) {
		getModel().setDateStart(datestart);
	}
	
	public void changeDateFinish(Date datefinish) {
		getModel().setDateFinish(datefinish);
	}
	
	public void changeDescription(String description) {
		getModel().setDescription(description);
	}
	
	public void changeTypeMaintenance(MaintenanceType maintenancetype) {
		getModel().setMaintenanceType(maintenancetype);
	}
	
	public void changeCause(Cause cause) {
		getModel().setCause(cause);
	}
	
	public void changeTeam(Set<Team> team) {
		getModel().setTeamList(team);
	}
	
	public void persistInsert() {
		daoFactory.beginTransaction();
		daoFactory.createMaintenanceDao().insert(getModel());
		daoFactory.commitTransaction();
	}
	
	public void persistUpdate() {
		daoFactory.beginTransaction();
		daoFactory.createMaintenanceDao().update(getModel());
		daoFactory.commitTransaction();
	}
	
	public void persistDelete(Maintenance maintenance) {
		daoFactory.beginTransaction();
		daoFactory.createMaintenanceDao().delete(maintenance);
		daoFactory.commitTransaction();
	}
	
	public List<Maintenance> persistSearchAllMaintenance() {
		daoFactory.beginTransaction();
		List<Maintenance> listMaintenance = daoFactory.createMaintenanceDao().searchAll();
		daoFactory.commitTransaction();
		return listMaintenance;
	}
	
	public List<Mesh> persistSearchAll() {
		daoFactory.beginTransaction();
		List<Mesh> listUnit = daoFactory.createMeshDao().searchAll();
		daoFactory.commitTransaction();
		return listUnit;
	}
	
	public List<Unit> persistSearchAllUnit() {
		daoFactory.beginTransaction();
		List<Unit> listUnit = daoFactory.createUnitDao().searchAll();
		daoFactory.commitTransaction();
		return listUnit;
	}
	
	public List<Plant> persistSearchPlantByUnit(Unit unit) {
		daoFactory.beginTransaction();
		List<Plant> listPlant = daoFactory.createPlantDao().searchByUnit(unit);
		daoFactory.commitTransaction();
		return listPlant;
	}
	
	public List<Mesh> persistSearchMeshByPlant(Plant plant) {
		daoFactory.beginTransaction();
		List<Mesh> listMesh = daoFactory.createMeshDao().searchByPlant(plant);
		daoFactory.commitTransaction();
		return listMesh;
	}
	
	public List<Device> persistSearchAllDeviceTag(Mesh example) {
		daoFactory.beginTransaction();
		List<Device> listDevice = daoFactory.createDeviceDao().searchByMesha(example);
		daoFactory.commitTransaction();
		return listDevice;
	}
	
	public List<MaintenanceType> persistSearchAllMaintenanceType() {
		daoFactory.beginTransaction();
		List<MaintenanceType> listMaintenanceType = daoFactory.createMaintenanceTypeDao().searchAll();
		daoFactory.commitTransaction();
		return listMaintenanceType;
	}
	
	public List<Cause> persistSearchAllCause() {
		daoFactory.beginTransaction();
		List<Cause> listCause = daoFactory.createCauseDao().searchAll();
		daoFactory.commitTransaction();
		return listCause;
	}
	
	public List<Mesh> persistSearchByExample(Mesh example) {
		daoFactory.beginTransaction();
		List<Mesh> meshList = daoFactory.createMeshDao().searchByExample(example);
		daoFactory.commitTransaction();
		return meshList;
	}
	
	public List<Maintenance> persistSearchByExample(Maintenance example) {
		daoFactory.beginTransaction();
		List<Maintenance> listMaintenance = daoFactory.createMaintenanceDao().searchByExample(example);
		daoFactory.commitTransaction();
		return listMaintenance;
	}
	
	public List persistSearchDatesByDevice() {
		daoFactory.beginTransaction();
		Date date1 = new GregorianCalendar(2013, 01, 01).getTime();
		Date date2 = new GregorianCalendar(2014, 01, 01).getTime();
		List listDate = daoFactory.createMaintenanceDao().searchByDevice2(date1,date2);
		daoFactory.commitTransaction();
		return listDate;
	}
	
	public List persistSearchByDevice(Device example) {
		daoFactory.beginTransaction();
		List<Date> listDate = daoFactory.createMaintenanceDao().searchByDevice3(example);
		daoFactory.commitTransaction();
		return listDate;
	}
	
	public List persistSearchByDevice2(Device example) {
		daoFactory.beginTransaction();
		List<Integer> listDate = daoFactory.createMaintenanceDao().searchByDevice4(example);
		daoFactory.commitTransaction();
		return listDate;
	}

	

}
